package com.bobo.controller;

import com.bobo.view.AlgoFrame;
import com.bobo.model.Circle;
import com.bobo.units.AlgoVisUnit;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;

/**
 * @Author: bobobo
 * @Date: 2019/9/3 11:34
 * @Version：1.0
 */
public class AlgoVisualizer {
    // 创建自己的数据
    private static int DELAY = 40;
    private int[] money;  //数据
    private AlgoFrame frame;   //视图


    public AlgoVisualizer(int sceneWidth,int sceneHeight){
        //初始化自己的数据
        //初始化数据
        money = new int[100];
       for (int i = 0;i < money.length;i++)
           money[i] = 100;

        //初始化视图
        // 事件队列 创建新线程===事件分发线程
        EventQueue.invokeLater(() -> {
            frame = new AlgoFrame("随机分钱问题的模拟",sceneWidth,sceneHeight);

            new Thread(() -> {
                run();
            }).start();
        });
    }

    //动画逻辑
    private void run(){
        //编写自己的动画逻辑
        while (true) {
            //排序
            Arrays.sort(money);

            //绘制数据
            frame.render(money);
            AlgoVisUnit.pause(DELAY);

            //优化过慢问题
            for (int k = 0;k < 30;k++){
                for (int i = 0; i < money.length; i++){
                    //if (money[i] > 0) {
                        int j = (int) (Math.random() * money.length);
                        money[i] -= 1;
                        money[j] += 1;
                    //}
                }
            }
        }
    }

}
